package cn.unknowpupil.init.service.system.impl;

import cn.unknowpupil.init.domain.system.SysDictData;
import cn.unknowpupil.init.service.system.ISysDictDataService;
import cn.unknowpupil.init.utils.strings.StringUtils;
import cn.unknowpupil.init.domain.system.SysDictType;
import cn.unknowpupil.init.mapper.system.SysDictTypeMapper;
import cn.unknowpupil.init.service.system.ISysDictTypeService;
import cn.unknowpupil.init.utils.strings.DictUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created at 2020-10-30 by  w10g <br>
 * Initialize SysDictType  ... <br>
 * 字典类型表 核心业务处理实现类
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 新增
     *
     * @param sysDictType {@link SysDictType}  字典类型表
     * @return {@link SysDictType}
     */
    @Override
    public boolean create(SysDictType sysDictType) {
        return save(sysDictType);
    }

    /**
     * 详情
     *
     * @param id {@code Long}   SysDictType ID
     * @return {@link SysDictType}
     */
    @Override
    public SysDictType info(Long id) {
        return getById(id);
    }

    /**
     * 删除
     *
     * @param ids {@code Long}   SysDictType ID
     * @return {@link SysDictType}
     */
    @Override
    public boolean delete(Long[] ids) {
        List<Long> collect = Stream.of(ids).collect(Collectors.toList());
        return removeByIds(collect);
    }


    /**
     * 修改
     *
     * @param sysDictType {@link SysDictType}  字典类型表
     * @return {@link SysDictType}
     */
    @Override
    public boolean update(SysDictType sysDictType) {
        SysDictType byId = super.getById(sysDictType.getId());
        List<SysDictData> list = dictDataService.list(new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getDictType, byId.getDictType()));
        list.forEach(o->{
            o.setDictType(sysDictType.getDictType());
        });
        if(list.size()>0){
            dictDataService.updateBatchById(list);
        }
        return updateById(sysDictType);
    }


    /**
     * 分页
     *
     * @param current     {@code int} 页码
     * @param size        {@code int} 笔数
     * @param sysDictType {@link SysDictType}
     * @return {@code IPage<SysDictType>}
     */
    @Override
    public IPage<SysDictType> page(int current, int size, SysDictType sysDictType) {
        IPage<SysDictType> page = new Page<>(current, size);
        LambdaQueryWrapper<SysDictType> wrapper = wrapper(sysDictType);
        return super.page(page, wrapper);
    }

    @Override
    public void clearCache() {
        DictUtils.clearDictCache();
    }

    @Override
    public List<SysDictType> selectDictTypeList(SysDictType sysDictType) {
        LambdaQueryWrapper<SysDictType> wrapper = wrapper(sysDictType);
        return super.list(wrapper);
    }

    private LambdaQueryWrapper<SysDictType> wrapper(SysDictType sysDictType) {
        LambdaQueryWrapper<SysDictType> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sysDictType.getDictName())) {
            wrapper.like(SysDictType::getDictName, sysDictType.getDictName());
        }
        if (StringUtils.isNotBlank(sysDictType.getDictType())) {
            wrapper.like(SysDictType::getDictType, sysDictType.getDictType());
        }
        if (StringUtils.isNotBlank(sysDictType.getStatus())) {
            wrapper.eq(SysDictType::getStatus, sysDictType.getStatus());
        }
        return wrapper;
    }

}
